package com.lesso.lzh.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.core.ApiResponse;
import com.lesso.lzh.sunflower.pojo.dto.EmployeeAttendanceRuleDTO;
import com.lesso.lzh.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import com.lesso.lzh.sunflower.pojo.vo.EmployeeVO;
import com.lesso.lzh.sunflower.service.IEmployeeAttendanceRuleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 员工考勤规则关系 前端控制器
 * </p>
 *
 * @author zqz
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/rel/employee-attendance-rule")
public class EmployeeAttendanceRuleController {

    @DubboReference
    private IEmployeeAttendanceRuleService employeeAttendanceRuleService;

    /**
     * 新增员工考勤规则关系
     */
    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody EmployeeAttendanceRuleDTO dto) {
        try {
            return ApiResponse.<Long>ok().data(employeeAttendanceRuleService.save(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Long>error().msg("新增员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }

    /**
     * 分页查询（默认分页）
     */
    @GetMapping("/page")
    public ApiResponse<Page<EmployeeAttendanceRuleVO>> page(EmployeeAttendanceRuleQueryDTO dto) {
        try {
            return ApiResponse.<Page<EmployeeAttendanceRuleVO>>ok()
                    .data(employeeAttendanceRuleService.page(dto))
                    .build();
        } catch (Exception e) {
            return ApiResponse.<Page<EmployeeAttendanceRuleVO>>error().msg("分页查询员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }

    /**
     * 分页查询（自定义分页）
     */
    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeAttendanceRuleVO>> customPage(EmployeeAttendanceRuleQueryDTO dto) {
        try {
            return ApiResponse.<Page<EmployeeAttendanceRuleVO>>ok().data(employeeAttendanceRuleService.customPage(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Page<EmployeeAttendanceRuleVO>>error().msg("自定义分页查询员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }

    /**
     * 根据ID删除
     */
    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        try {
            employeeAttendanceRuleService.deleteById(id);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("删除员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }

    /**
     * 根据ID查询
     */
    @GetMapping
    public ApiResponse<Object> selectById(@RequestParam("id") Long id) {
        try {
            Object result = employeeAttendanceRuleService.selectById(id);
            return ApiResponse.ok().data(result).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("查询员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }

    /**
     * 更新员工考勤规则关系
     */
    @PutMapping
    public ApiResponse<Object> update(@RequestBody EmployeeAttendanceRuleDTO dto) {
        try {
            employeeAttendanceRuleService.update(dto);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("更新员工考勤规则关系失败: " + e.getMessage()).build();
        }
    }
}